<?php
    session_start();
    include_once '../../../../controlador/php/conexion/Conexion.php';
    $con = new Conexion();
    
    $oid = $_REQUEST['oid'];
    $aut = $_REQUEST['aut'];
    $usuario = $_SESSION['usuario'];

        $count ="select COUNT(*) as n from segimagenes where hcnsolexa = '".$oid."'";
        $res = $con->query($count);
    
    while($res2 = mssql_fetch_array($res)){
        $n = $res2['n'];
    }
    if ($n == 0){
        if($usuario == ""){
            print_r(json_encode(array('n'=>1)));
        }else{
            if ($aut == 1){
                $inser= "insert into segimagenes(hcnsolexa,autorizar,fecauto,usu_oidau)values('".$oid."','1',GETDATE(),'".$_SESSION['usuario']."')";
                $con->query($inser);
            }else if ($aut ==2){
                $inser= "insert into segimagenes(hcnsolexa,autorizarn,fecauton,usu_oidaun)values('".$oid."','1',GETDATE(),'".$_SESSION['usuario']."')";
                $con->query($inser);
            }else if ($aut ==3){
                $inser= "insert into segimagenes(hcnsolexa,autorizarno,fecautono,usu_oidauno)values('".$oid."','1',GETDATE(),'".$_SESSION['usuario']."')";
                $con->query($inser);
            }
            print_r(json_encode(array('n'=>2)));
        }
    }
    else {
        if ($aut == 1){
            if($usuario == ""){
               print_r(json_encode(array('n'=>1)));
           }else{
                $update ="update segimagenes
                            set autorizar ='1',
                            fecauto = GETDATE(),
                            usu_oidau ='".$_SESSION['usuario']."'
                          where hcnsolexa ='".$oid."'";
                $con->query($update);
                print_r(json_encode(array('n'=>2)));
           }
        }else if ($aut ==2){
            if($usuario == ""){
                print_r(json_encode(array('n'=>1)));
                
            }
            else{
                $update ="update segimagenes
                        set autorizarn ='1',
                        fecauton = GETDATE(),
                        usu_oidaun ='".$_SESSION['usuario']."'
                     where hcnsolexa ='".$oid."'";
                $con->query($update);
                print_r(json_encode(array('n'=>2)));
            }
        }else if ($aut ==3){
            if($usuario == 1){
                print_r(json_encode(array('n'=>1)));
            }else{
            $update ="update segimagenes
                        set autorizarno ='1',
                        fecautono = GETDATE(),
                        usu_oidauno ='".$_SESSION['usuario']."'
                     where hcnsolexa ='".$oid."'";
        $con->query($update);
        print_r(json_encode(array('n'=>2)));
        }
   }
  }
    $con->cerrarConexion();
    
 
?>